package com.touchX.display
{
	import com.greensock.TweenMax;
	import com.greensock.easing.Circ;
	import com.touchX.utils.NumberUtil;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class FreedomDisplayObject extends AbstractDisplay
	{
		protected var initPoint:Point;
		protected var targetPoint:Point;
		
		private var duration:Number=1;
		
		private var outRectangle:Rectangle;
		private var stageRectangle:Rectangle;
		private var innerRectangle:Rectangle;
		private var rotationDegree:Number;
		
		
		protected var marginTop:int;
		protected var marginBottom:int;
		protected var marginLeft:int;
		protected var marginRight:int;
		protected var paddingTop:int;
		protected var paddingBottom:int;
		protected var paddingLeft:int;
		protected var paddingRight:int;
		
		
		
		public function FreedomDisplayObject()
		{
			super();
			marginTop=marginBottom=marginLeft=marginRight=500;
			paddingTop=paddingBottom=paddingLeft=paddingRight=10;
			
		}
		protected override function added(event:Event):void{
			stageRectangle=new Rectangle(0,0,stage.stageWidth,stage.stageHeight);
			outRectangle=new Rectangle(-marginLeft,-marginTop,stage.stageWidth+marginLeft+marginBottom-this.width,stage.stageHeight+marginTop+marginBottom-this.height);
			innerRectangle=new Rectangle(paddingLeft,paddingTop,stage.stageWidth-paddingLeft-paddingRight-this.width,stage.stageHeight-paddingTop-paddingBottom-this.height);
			
			
			initPoint= getPointOutStage();
			this.x=initPoint.x;
			this.y=initPoint.y;
			
			
			targetPoint=getTargetPoint();
			rotationDegree=NumberUtil.random(-30,30);
			//var degree:Number=0
			var duration:Number=Math.random()*5+.1;
			
			TweenMax.to(this,duration,{x:targetPoint.x,y:targetPoint.y,rotation:rotationDegree,delay:duration,ease:Circ.easeOut,onComplete:onAddedToStage});
			
			/*TweenMax.to(mc, 5, {delay:3, x:300, ease:Back.easeOut, onComplete:onFinishTween, onCompleteParams:[5, mc]});
			function onFinishTween(param1:Number, param2:MovieClip):void {
			trace("The tween has finished! param1 = " + param1 + ", and param2 = " + param2);
			}*/
			
			//addEvent(stage, Event.RESIZE, resize);
			super.added(event);
			addEvent(this,MouseEvent.MOUSE_OVER,activing);
			addEvent(this,MouseEvent.MOUSE_OUT,disactive);
		}
		protected function activing(evt:MouseEvent):void{
			this.parent.setChildIndex(this,this.parent.numChildren-1);
			TweenMax.to(this,duration,{rotation:0,ease:Circ.easeOut});
		}
		protected function disactive(evt:MouseEvent):void{
			TweenMax.to(this,duration,{rotation:rotationDegree,ease:Circ.easeOut});
		}
		
		private function getTargetPoint():Point{
			var pointX:int=NumberUtil.random(innerRectangle.left,innerRectangle.right);
			var pointY:int=NumberUtil.random(innerRectangle.top,innerRectangle.bottom);
			return new Point(pointX,pointY);
		}
		private function getPointOutStage():Point{
			var pointX:int=NumberUtil.random(outRectangle.left,outRectangle.right);
			var pointY:int=NumberUtil.random(outRectangle.top,outRectangle.bottom);
			var point:Point=new Point(pointX,pointY);
			if(stageRectangle.containsPoint(point)) getPointOutStage();
			return point;
		}
		
		
		
		
		
		private function onAddedToStage():void{
			initPoint=new Point(this.x,this.y);
		}
		
		protected override function resize(event:Event=null):void{
			innerRectangle=new Rectangle(paddingLeft,paddingTop,stage.stageWidth-paddingLeft-paddingRight,stage.stageHeight-paddingTop-paddingBottom);
			targetPoint=getTargetPoint();
			TweenMax.to(this,duration,{x:targetPoint.x,y:targetPoint.y,delay:Math.random(),ease:Circ.easeOut,onComplete:onAddedToStage});
		}
		public function spreadToRectangle(rect:Rectangle):void{
			var pointX:int=NumberUtil.random(rect.left,rect.right);
			var pointY:int=NumberUtil.random(rect.top,rect.bottom);
			TweenMax.to(this,duration,{x:pointX,y:pointY,delay:Math.random(),ease:Circ.easeOut});
			this.initPoint=new Point(pointX,pointY);
		}
		
	}
}